﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PopupManager.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   Managed popups.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PopupLibrary
{
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using Windows.UI.Xaml;

    /// <summary>Managed popups.</summary>
    public static class PopupManager
    {
        /// <summary>The active popups.</summary>
        private static readonly Stack<Popup> ActivePopups = new Stack<Popup>();

        /// <summary>Gets the current active popup.</summary>
        public static Popup Current
        {
            get
            {
                return ActivePopups.Count == 0 ? null : ActivePopups.Peek();
            }
        }

        /// <summary>Displays a popup.</summary>
        /// <param name="element">The element.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        public static async Task<object> ShowAsync(UIElement element, PopupSettings settings = null)
        {
            // apply defaults if not specified
            settings = settings ?? new PopupSettings();

            var popup = new Popup(element, settings);
            ActivePopups.Push(popup);
            try
            {
                object result = await popup.ShowAsync();
                return result;
            }
            finally
            {
                ActivePopups.Pop();
            }
        }
    }
}
